package com.twu.biblioteca;

import java.util.Scanner;

public class TopMenu extends Menu {
    private static final int LOGIN_AS_CUSTOMER = 1;
    private static final int LOGIN_AS_LIBRARIAN = 2;
    private static final int QUIT = 3;
    private CustomerAccountManager customerAccountManager;

    public TopMenu(BookList availableBooks, BookList checkoutBooks, MovieList availableMovies, MovieList checkoutMovies) {
        this.availableBooks = availableBooks;
        this.checkoutBooks = checkoutBooks;
        this.availableMovies = availableMovies;
        this.checkoutMovies = checkoutMovies;
        optionDescriptions = new String[]{"Login as customer", "Login as Librarian", "Quit"};
        customerAccountManager = new CustomerAccountManager("resources/customer.xml");
    }

    @Override
    public Menu handleOption(int option) {
        if (selectAWrongOption(option)) {
            return this;
        } else {
            if (option == QUIT) {
                System.exit(0);
            }
            if (option == LOGIN_AS_CUSTOMER) {
                return loginAsCustomer();
            } else {
                System.out.println("*******************  Welcome to Biblioteca!  *******************\n");
                return new LibrarianMenu(this.availableBooks, this.checkoutBooks, this.availableMovies, this.checkoutMovies);
            }
        }
    }

    private Menu loginAsCustomer() {
        System.out.print("Please Input Your Library Number: ");
        Scanner scanner = new Scanner(System.in);
        String libraryNumber = scanner.next();
        System.out.print("Please Input Your Password: ");
        String password = scanner.next();
        Customer customer = customerAccountManager.getCustomer(libraryNumber, password);
        if (customer == null) {
            System.out.println("Your Input Information is Error.\n");
            return this;
        } else {
            System.out.println("*******************  Welcome to Biblioteca!  *******************\n");
            return new CustomerMenu(this.availableBooks, this.checkoutBooks, this.availableMovies, this.checkoutMovies, customer);
        }
    }
}
